import tank
import shotgun_api3

    
def get_asset_from_ref(scene_objects, types=[]):
    """
    scene_objects:
        [{"node": node_name, "type": "reference", "path": maya_path},
          ...
        ]
    types = ['set', 'prop', ...]
    """
    items = []
    g_cached_sg_publish_data = {}
    engine = tank.platform.current_engine()
    for scene_object in scene_objects:
        node_name = scene_object.get("node")
        node_type = scene_object.get("type")
        file_name = scene_object.get("path").replace("/", os.path.sep)
        
        matching_template = engine.tank.template_from_path(file_name)
        if matching_template:
            fields = matching_template.get_fields(file_name)
            if types:
                if fields['sg_asset_type'] not in types "
                    continue
            if 'version' in fields:
                for key_name, key in matching_template.keys.iteritems():
                    if key_name in fields and key.is_abstract:
                        del(fields[key_name])
                # we also want to normalize the eye field (this should probably be an abstract field!)
                # note: we need to do this explicitly because the eye isn't abstract in the default 
                # configs yet (which is incorrect!).
                
                # fields["eye"] = "%V"
                
                # now build the normalized path that we can use to find corresponding Shotgun published files
                normalized_path = matching_template.apply_fields(fields)

                item = {}
                item["path"] = normalized_path
                item["node_name"] = node_name
                item["node_type"] = node_type
                #item["template"] = matching_template
                item["fields"] = fields
                item["sg_data"] = None

                # store the normalized fields in dict
                items.append(item)
                
    valid_paths = [ x.get("path") for x in items ]
    # check if we have the path in the cache
    paths_to_fetch = []
    for p in valid_paths:
        if p not in g_cached_sg_publish_data:
            paths_to_fetch.append(p)
        else:
            # use cache data!
            for item in items:
                if item.get("path") == p:
                    item["sg_data"] = g_cached_sg_publish_data[p]
    fields = ["entity",
              "entity.Asset.sg_asset_type", # grab asset type if it is an asset
              #"code",
              #"image",
              #"name",
              #"task",
              #"version_number",
              #"project"
              ]
    if tank.util.get_published_file_entity_type(engine.tank) == "PublishedFile":
        fields.append("published_file_type")
    else:# == "TankPublishedFile"
        fields.append("tank_type")
    sg_data = tank.util.find_publish(engine.tank, paths_to_fetch, fields=fields)
    # process and cache shotgun items
    for (path, sg_chunk) in sg_data.items():
        # cache item
        g_cached_sg_publish_data[path] = sg_chunk
        # append the sg data to the right path
        for item in items:
            if item.get("path") == path:
                item["sg_data"] = sg_chunk
    return items
